package com.exam.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


import com.exam.dao.UserDao;
import com.exam.entity.User;
import com.exam.util.PageBean;
import com.google.gson.Gson;

/**
 * 用户的所有操作
 */
@WebServlet("/admin/student")
public class UserServlet extends BaseServlet {
	UserDao dao = new UserDao();
	Gson gson=new Gson();
	String p=null;
	// 查一页数据
	protected void page(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("查一页数据");  // ?m=page&p=4
		//字符乱码修复
		response.setCharacterEncoding("utf-8");
        response.setContentType("text/html");
        request.setCharacterEncoding("utf-8");
		p = request.getParameter("p");  // 你要看的页面
		if (p==null) {
			p="1";
		}
		int intp =  Integer.parseInt(p);  // 根据当前页，算出开始结束的页码
		
		List<User> userlist=null;
		try {
			userlist = dao.show(intp);  // 查选定页数据
			for (User user : userlist) {
				user.setClass_name(dao.selClass(user.getClass_id()));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		String data=gson.toJson(userlist);
		PrintWriter out=response.getWriter();
		out.print(data);
	}
	//传学生总数
	protected void count(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
		int count=dao.count();
		String json=gson.toJson(count);
		PrintWriter out=response.getWriter();
		out.write(json);
	}
	// ID 查一个人的数据
	protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("查一个人的数据");
		//字符乱码修复
		response.setCharacterEncoding("utf-8");
	    response.setContentType("text/html");
	    request.setCharacterEncoding("utf-8");
	    
		String id = request.getParameter("id");
		int userid=0;
		List<User> userlist=null;
		try {
			userid=Integer.parseInt(id);
			userlist =dao.selUser(userid);
			for (User user : userlist) {
				user.setClass_name(dao.selClass(user.getClass_id()));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		String data=gson.toJson(userlist);
		System.out.println(data);
		System.out.println(userlist.get(0));
		PrintWriter out=response.getWriter();
		out.print(data);
	}
	//创建用户获取信息
	protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        req.setCharacterEncoding("utf-8");
        
        int flag=0;
        System.out.println("开始注册账号");

        String user_name = req.getParameter("user");
        
        byte sz[]=user_name.getBytes("iso8859-1");
		String username = new String(sz,"utf-8");
        
        int role_id = Integer.parseInt(req.getParameter("limit"));
        int class_id = Integer.parseInt(req.getParameter("classes"));
        String user_password = req.getParameter("password");
        String user_phone = req.getParameter("phone");
        String user_sex = req.getParameter("sex");
        
        byte sz1[]=user_sex.getBytes("iso8859-1");
		String usersex = new String(sz1,"utf-8");
        
        String user_wx = req.getParameter("weixin");
        String user_desc = req.getParameter("desc");
        
        byte sz2[]=user_desc.getBytes("iso8859-1");
		String userdesc = new String(sz2,"utf-8");
        
        String user_avatar =req.getParameter("photo");
        System.out.println(user_avatar);
        
        User users = new User(role_id, class_id, username, user_password,
        		user_phone, usersex, user_wx, userdesc, user_avatar);

        System.out.println(users);

        try {
            flag = dao.registerUser(users);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("插入失败！。。。");
        }

        if (flag==1){
            System.out.println("注册成功");
            resp.sendRedirect("/OnlineExam1/admin/student_manage.jsp");
        }
	}
	//创建用户时班级名称查询
	protected void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		List classlist=null;
		List roleList=null;
        try {
        	classlist = dao.selClass();
        	roleList = dao.selRole();
        	
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("插入失败！。。。");
        }
        req.setAttribute("classlist", classlist);
        req.setAttribute("roleList", roleList);
        req.getRequestDispatcher("/admin/create_user.jsp").forward(req, resp);
	}
	//通过用户id删除用户
	protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		System.out.println("user del"+id);
		int userid=0;
		try {
			userid=Integer.parseInt(id);
			dao.del(userid);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		//去哪？？ 去查第一页
		response.sendRedirect("/OnlineExam1/admin/student_manage.jsp");
		
	}
	//修改用户前传用户数据给表单
	protected void update(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		System.out.println("user update");
		String userid = request.getParameter("uid");
		int id=Integer.parseInt(userid);
		List<User> userlist=null;
		List classlist=null;
		List roleList=null;
		try {
			classlist = dao.selClass();
        	roleList = dao.selRole();
			userlist = dao.selUser(id);//查一个人信息
			for (User user : userlist) {
				user.setClass_name(dao.selClass(user.getClass_id()));
				user.setRole_name(dao.selRole(user.getRole_id()));
				System.out.println(user.getClass_name()+"--"+user.getRole_name());
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 传给JSP  ---》 request
		request.setAttribute("classlist", classlist);
		request.setAttribute("roleList", roleList);
		request.setAttribute("apage", userlist);
		request.getRequestDispatcher("/admin/modify_user.jsp").forward(request, response);
	}
	
	//修改用户后数据处理
	protected void modify(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/html");
        req.setCharacterEncoding("utf-8");
        
        int flag=0;
        System.out.println("修改账号");

        String user_name = req.getParameter("user");
        
        byte sz[]=user_name.getBytes("iso8859-1");
		String username = new String(sz,"utf-8");
        
        int role_id = Integer.parseInt(req.getParameter("limit"));
        int class_id = Integer.parseInt(req.getParameter("classes"));
        String user_password = req.getParameter("password");
        String user_phone = req.getParameter("phone");
        String user_sex = req.getParameter("sex");
        
        byte sz1[]=user_sex.getBytes("iso8859-1");
		String usersex = new String(sz1,"utf-8");
        
        String user_wx = req.getParameter("weixin");
        String user_desc = req.getParameter("desc");
        
        byte sz2[]=user_desc.getBytes("iso8859-1");
		String userdesc = new String(sz2,"utf-8");
        
        String user_avatar = req.getParameter("photo");
        
        User users = new User(role_id, class_id, username, user_password,
        		user_phone, usersex, user_wx, userdesc, user_avatar);
        users.setUser_id(Integer.parseInt(req.getParameter("id")));
        System.out.println(users);

        try {
            flag = dao.modifyUser(users);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("插入失败！。。。");
        }

        if (flag==1){
            System.out.println("修改成功");
            resp.sendRedirect("/OnlineExam1/admin/student_manage.jsp");
        }
	}
	
}
